<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/29
 * Time: 9:18
 */

class Involved_ProductCateModel extends BaseModel
{
    protected $table = 'product_cate';

    /**
     * 获取产品分类信息
     * return ProductCate
     */
    public function GetGoodsCateList()
    {
        return DB::table($this->table)->paginate(8);
    }

    /**
     * 获取添加产品分类列表
     * @return mixed
     */
    public function GetProductCateList()
    {
        return DB::table($this->table)->get();
    }

    public function GetGoodsCateInfo($id)
    {
        return DB::table($this->table)->where('cate_id',$id)->first();
    }

    public function GetListByParentId($id, $filed = array())
    {
        $db = DB::table($this->table);
        if (!empty($filed)) {
            $db->select($filed);
        }
        return $db->where('parent_id',$id)->get();
    }


    public function GetProductCateDetail($classes)
    {
        return DB::table($this->table)
                ->select('cate_name','introduce')
                ->where('cate_id',$classes)
                ->first();
    }

    /**
     * 获取产品材质
     * @return mixed
     * @author alice
     * @date 2016-1-26
     */
    public function GetQualityList()
    {
        return DB::table($this->table)
            ->select('cate_id','cate_name')
            ->where('length',ProductEnum::PRODUCT_LEVEL_THREE)
            ->get();
    }
    public function GetName($cate_id){
        return DB::table($this->table)
            ->select('cate_name')
            ->where('cate_id',$cate_id)
            ->first();
    }

    /**
     * 获取产品分类
     * @param $quality
     * @return mixed
     * @author alice
     * @date 2016-2-21
     */
    public function GetCategoryById($quality)
    {
        return DB::table($this->table)
            ->select('cate_id','cate_name','length')
            ->where('parent_id',$quality)
            ->first();
    }

    /**
     * 获取产品品类
     * @param $category
     * @return mixed
     * @author alice
     * @date 2016-2-21
     */
    public function GetClassesById($category)
    {
        return DB::table($this->table)
            ->select('cate_id','cate_name','length')
            ->where('parent_id',$category)
            ->first();
    }

    /**
     * 获取产品分类
     * @return mixed
     * @author alice
     * @date 2016-2-24
     */
    public function GetProductCategory()
    {
        return DB::table($this->table)
            ->select('cate_id','cate_name')
            ->where(['parent_id' => 0, 'length' => 1,'status' => 1])
            ->orderBy('rank')
            ->get();
    }

    #获取品类
    public function GetProductClasses($cate_id)
    {
        return DB::table($this->table)
            ->select('cate_id','cate_name')
            ->where(['parent_id' => $cate_id, 'length' => 2,'status' => 1])
            ->orderBy('rank')
            ->get();
    }

    /**
     * 获取上级分类ID
     * @param $cate_id
     * @return mixed
     * @author buluota
     * @date 2016-03-08
     */
    public function GetParentCate($cate_id)
    {
        $cate = DB::table($this->table)
            ->select('cate_id','cate_name','parent_id')
            ->where('cate_id',$cate_id)
            ->first();
        return empty($cate) ? NULL : $cate->parent_id;
    }


    /**
     * 获取材质信息
     * @param $cate_id
     * @return mixed
     * @author buluota
     * @date 2016-03-08
     */
    public function GetProductQuality($cate_id)
    {
        return DB::table($this->table)
            ->select('cate_id','cate_name','parent_id')
            ->where(['parent_id' => $cate_id, 'length' => 3,'status' => 1])
            ->orderBy('rank')
            ->get();
    }
    /**
     * 获取分类列表
     * @author fyl
     * @date 2016-04-04
     */
    public function AdminProductCateAllList()
    {
        return DB::table($this->table)
            ->selectRaw('sc_product_cate.cate_id, sc_product_cate.cate_name, sc_product_cate.length, sc_product_cate.remark, sc_cate1.cate_name as parent_id')
            ->leftJoin('product_cate as sc_cate1','product_cate.parent_id','=','cate1.cate_id')
            ->get();
    }




    /**
     * 获取分类列表
     * @author fyl
     * @date 2016-04-04
     */
    public function AdminProductCateList()
    {
        return DB::table($this->table)
            ->where('length','1')
            ->get();
    }



    /**
     * 获取材质列表
     * @author fyl
     * @date 2016-04-04
     */
    public function AdminProductCateQualityList()
    {
        return DB::table($this->table)
            ->where('length','1')
            ->get();
    }

    /**
     * 获取品类列表
     * @author fyl
     * @date 2016-04-04
     */
    public function AdminProductCateClassesList()
    {
        return DB::table($this->table)
            ->where('length','2')
            ->get();
    }

    /**
     * 根据ID获取产品详情
     * @return mixed
     */
    public function GetProductCateById($id)
    {
        return  DB::table($this->table)
            ->where('product_cate.cate_id',$id)
            ->first();
    }

    /**
     * 根据ID获取其parent_ID详情
     * @return mixed
     */
    public function GetProductCateParentId($id)
    {
        return  DB::table($this->table)
            ->select('parent_id')
            ->where('product_cate.cate_id',$id)
            ->first();
    }

    /**
     * 根据ID获取产品父类详情
     * @return mixed
     */
    public function GetProductCateParent($id)
    {
        return  DB::table($this->table)
            ->where('product_cate.parent_id',$id)
            ->first();
    }
    /**
     * 修改分类
     */
    public function UpdateProductCate($cate_id,$aData){
        return DB::table($this->table)->where(['cate_id' =>$cate_id])->update($aData);


    }
    public function yiji(){
        return DB::table($this->table)
            ->where('parent_id','0')
            ->get();
    }
    public function erji($parent_id){
        return DB::table($this->table)
            ->where('parent_id',$parent_id)
            ->get();
    }
    public function sanji($parent_id){
        return DB::table($this->table)
            ->where('parent_id',$parent_id)
            ->get();
    }
   public function linkage($id){
       return DB::table($this->table)
           ->where('parent_id',$id)
           ->get();

   }

}